
package com.xiaoma.myaudience.biz.adapter;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.Gallery.LayoutParams;

import com.xiaoma.myaudience.R;
import com.xiaoma.myaudience.biz.model.ActivityDetailInfoModel;
import com.xiaoma.myaudience.biz.view.LoadingImageView;
import com.xiaoma.myaudience.util.PicUtils;
import com.xiaoma.myaudience.util.model.ModelUtils;

public class ActivitylineAdapter extends BaseAdapter {

    private LayoutInflater mInflater;
    private List<Map<String, Object>> mList;
    private String mSizeStr;

    public ActivitylineAdapter(Context context, List<Map<String, Object>> list) {
        mInflater = LayoutInflater.from(context);
        mList = list;
        mSizeStr = PicUtils.composeSize((int) context.getResources().getDimension(
                R.dimen.headline_img_width), (int) context.getResources().getDimension(
                R.dimen.headline_img_height));
    }

    /**
     * 根据position,返回这个位置对应的比较大的中间的位置.
     * @param position
     * @return
     */
    public int getInitPosition(int position) {
        if (position < 0) {
            return position;
        }
        
        final int realCount = getRealCount();
        if (realCount <= 0) {
            return position;
        }
        
        position = getRealPosition(position);
        return Integer.MAX_VALUE / 2 - (Integer.MAX_VALUE / 2) % realCount + position;
    }
    
    /**
     * 返回在列表中的真实position.
     * @param position
     * @return
     */
    public int getRealPosition(int position) {
        if (position < 0) {
            return position;
        }
        final int realCount = getRealCount();
        if (realCount <= 0) {
            return position;
        }
        return position % getRealCount();
    }
    
    /**
     * 返回在列表真实count.
     * @param position
     * @return
     */
    public int getRealCount() {
        return mList.size();
    }
    
    @Override
    public int getCount() {
        return getRealCount() > 0 ? Integer.MAX_VALUE : 0;
    }

    @Override
    public Object getItem(int position) {
        return mList.get(getRealPosition(position));
    }

    @Override
    public long getItemId(int position) {
        return getRealPosition(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewparent) {
        position = getRealPosition(position);
        if (getRealCount() == 0) {
            return null;
        }
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.headline_item, null);
            convertView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT,
                    LayoutParams.FILL_PARENT));
            ViewHolder holder = new ViewHolder();
            holder.headlineLoadingImageView = (LoadingImageView) convertView
                    .findViewById(R.id.headline_liv);
            convertView.setTag(holder);
        }

        ViewHolder holder = (ViewHolder) convertView.getTag();

        Map<String, Object> infoMap = (Map<String, Object>) getItem(position);
        holder.headlineLoadingImageView.setLoadingDrawable(
        		ModelUtils.getStringValue(infoMap, ActivityDetailInfoModel.PARAM_PATH),
        		mSizeStr);
        return convertView;
    }

    private class ViewHolder {
        LoadingImageView headlineLoadingImageView;
    }

}
